package gold.gold01;

/**
 * 中等
 */
public class S0105一次编辑 {
    /**
     * 19, 59, 怎么这么慢。因为LeetCode时间精度问题, 所以即使是19, 也可能是因为只与大部队相差1ms,
     * 所以在用时很短或者O(n)复杂度的任务之下, 不比过高要求。
     * 从两个字符串的长度差之间入手, 长度差表明了二者之间可能的操作
     * 如果是两次编辑, 应该会难很多
     */
    public boolean oneEditAway3(String first, String second) {
        int lenDif = 0;
        if((lenDif = first.length() - second.length()) > 1 || lenDif < -1) return false;

        char[] firstList = first.toCharArray();
        char[] secondList = second.toCharArray();
        if(lenDif == 0){
            for(int i = 0; i < first.length() - 1; i ++){
                if(firstList[i] != secondList[i])
                    return first.substring(i + 1).equals(second.substring(i + 1));
            }
        }
        int minLen = Math.min(first.length(), second.length());
        if(lenDif == 1){
            for(int i = 0; i < minLen; i ++){
                if(firstList[i] != secondList[i])
                    return first.substring(i + 1).equals(second.substring(i));
            }
        }
        if(lenDif == -1){
            for(int i = 0; i < minLen; i ++){
                if(firstList[i] != secondList[i])
                    return first.substring(i).equals(second.substring(i + 1));
            }
        }
        System.out.println("最后一个修改了?");
        return true;
    }

    // StringBuilder版本, 13, 61 更惨了
    public boolean oneEditAway2(String first, String second) {
        int lenDif = 0;
        if((lenDif = first.length() - second.length()) > 1 || lenDif < -1) return false;

        StringBuilder firstSB = new StringBuilder(first);
        StringBuilder secondSB = new StringBuilder(second);

        if(lenDif == 0){
            for(int i = 0; i < first.length() - 1; i++){
                if(firstSB.charAt(i) != secondSB.charAt(i)){
                    return firstSB.substring(i + 1).equals(secondSB.substring(i + 1));
                }
            }
        }
        int minLen = Math.min(first.length(), second.length());
        if(lenDif == 1){
            for(int i = 0; i < minLen; i++){
                if(firstSB.charAt(i) != secondSB.charAt(i)){
                    return firstSB.substring(i + 1).equals(secondSB.substring(i));
                }
            }
        }
        if(lenDif == -1){
            for(int i = 0; i < minLen; i++){
                if(firstSB.charAt(i) != secondSB.charAt(i)){
                    return firstSB.substring(i).equals(secondSB.substring(i + 1));
                }
            }
        }

        System.out.println("最后一个修改了?");
        return true;
    }

    // 直接使用string的charAt方法, 不用toCharArray 19, 18, 没有办法
    public boolean oneEditAway(String first, String second) {
        int lenDif = 0;
        if((lenDif = first.length() - second.length()) > 1 || lenDif < -1) return false;

        if(lenDif == 0){
            for(int i = 0; i < first.length() - 1; i ++){
                if(first.charAt(i) != second.charAt(i))
                    return first.substring(i + 1).equals(second.substring(i + 1));
            }
        }
        int minLen = Math.min(first.length(), second.length());
        if(lenDif == 1){
            for(int i = 0; i < minLen; i ++){
                if(first.charAt(i) != second.charAt(i))
                    return first.substring(i + 1).equals(second.substring(i));
            }
        }
        if(lenDif == -1){
            for(int i = 0; i < minLen; i ++){
                if(first.charAt(i) != second.charAt(i))
                    return first.substring(i).equals(second.substring(i + 1));
            }
        }
        System.out.println("最后一个修改了?");
        return true;
    }


    public static void main(String[] args) {
        System.out.println(new S0105一次编辑().oneEditAway("apadsf", "apaled"));
    }
}
